package com.compass.doctor.ui.patient

import android.os.Bundle
import android.view.View
import android.view.inputmethod.EditorInfo
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.compass.doctor.base.BaseVBActivity
import com.compass.doctor.databinding.ActivityDiagnoseBinding
import com.compass.doctor.dialog.AppendDiagnoseDialog
import com.compass.doctor.ui.patient.viewmodel.ExamineViewModel
import com.compass.doctor.ui.prescription.adapter.DiagnoseAdapter
import com.compass.framework.constant.KEY_CONSULT_TYPE
import com.compass.framework.decoration.NormalItemDecoration
import com.compass.framework.ext.gone
import com.compass.framework.ext.onClick
import com.compass.framework.ext.visible
import com.compass.framework.model.Diagnosis
import com.compass.framework.utils.dpToPx

/**
 * Description： 查询标本和部位
 * Date:2024/1/31 15:03
 */
class SampleBodyActivity : BaseVBActivity<ActivityDiagnoseBinding>() {

    private val examineViewModel: ExamineViewModel by viewModels()

    private lateinit var mAdapter: DiagnoseAdapter
    private var allList: MutableList<Diagnosis> = mutableListOf()
    private var searchList: MutableList<Diagnosis> = mutableListOf()
    private var type: String? = null//标本类型 1-标本 2-部位 再换个理解方式 1-检验 2-检查

    override fun initView(savedInstanceState: Bundle?) {
        type = intent.serializable(KEY_CONSULT_TYPE)
        when (type) {
            "1" -> {
                mBinding.tvTitleBar.setMiddleText("标本类型")
                mBinding.edtDiagnosis.hint = "请输入标本关键字"
            }

            else -> {
                mBinding.tvTitleBar.setMiddleText("选择部位")
                mBinding.edtDiagnosis.hint = "请输入部位关键字"
            }
        }
        mBinding.edtDiagnosis.setOnEditorActionListener { _, actionId, _ ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                val keyWord = mBinding.edtDiagnosis.text.toString().trim()
                getInspectionItem(keyWord)
            }
            false
        }
        mBinding.tvTitleBar.getRightLayout().onClick {
            AppendDiagnoseDialog.Builder(this).show()
        }

        mBinding.btnSearch.onClick {
            val keyWord = mBinding.edtDiagnosis.text.toString().trim()
            getInspectionItem(keyWord)
        }
        initRecyclerView()
    }

    override fun initData() {
        examineViewModel.sampleBodyLiveData.observe(this) {
            dismissLoading()
            it?.let { list ->
                allList = list
                mAdapter.setData(allList)
            }

        }
        showLoading()
        examineViewModel.getSampleBody(type)

    }

    private fun getInspectionItem(keyword: String = "") {
        if (keyword.isEmpty()){
            showToast("请先输入搜索条件")
            return
        }
        searchList.clear()
        for (item in allList){
            if (item.name.contains(keyword)){
                searchList.add(item)
            }
        }
        if (searchList.isEmpty()){
            mAdapter.setData(null)
            mBinding.emptyView.visible()
        }  else{
            mBinding.emptyView.gone()
            mAdapter.setData(searchList)
        }
        dismissLoading()

    }


    private fun initRecyclerView() {
        mBinding.refreshLayout.apply {
            setEnableRefresh(false)
            setEnableLoadMore(false)

        }
        mAdapter = DiagnoseAdapter()
        val dp1 = dpToPx(1)
        mBinding.recyclerView.apply {
            layoutManager = LinearLayoutManager(context)
            addItemDecoration(NormalItemDecoration().apply {
                setBounds(top = dp1, bottom = dp1)
                setLastBottom(true)

            })
            adapter = mAdapter
        }
        mAdapter.onItemClickListener = { _: View, position: Int ->
            val item = mAdapter.getItem(position)
            item?.let {
                intent.putExtra("diagnosis", item)
                setResult(RESULT_OK, intent)
                finish()
            }

        }

    }
}